﻿using System.Collections.Generic;
using GameEngine.Clients.Silverlight.View;
using GameEngine.Model;
using MySettlers.View.Components;
using PureMVC.Interfaces;
using GameEngine.View.Components;
using GameEngine.Clients.Silverlight.View.Components;
using System.Windows;
using GameEngine.Model.Data;
using MySettlers.Things;
using GameEngine.View;

namespace MySettlers.View
{
    public class MyGameMediator : SilverlightGameMediator
    {
        public MyGameMediator(IGameDataProxy<int> dataProxy,
            //SilverlightLevelMediator<MyLevelUC> levelMediator,
            IGameView<int, FrameworkElement> view)
            : base(dataProxy, view) 
        {
        }

        public new MyGameUC ViewComponent
        {
            get { return base.ViewComponent as MyGameUC; }
        }

        #region IMediator Members

        /// <summary>
        /// List the <c>INotification</c> names this <c>Mediator</c> is interested in being notified of
        /// </summary>
        /// <returns>The list of <c>INotification</c> names </returns>
        override public IList<string> ListNotificationInterests()
        {
            IList<string> notifications = new List<string>(base.ListNotificationInterests());
            notifications.Add(DesktopMediator.KEY_DOWN);
            notifications.Add(DesktopMediator.KEY_UP);
            return notifications;
        }

        /// <summary>
        /// Handle <c>INotification</c>s
        /// </summary>
        /// <param name="notification">The <c>INotification</c> instance to handle</param>
        /// <remarks>
        ///     <para>
        ///        Typically this will be handled in a switch statement, with one 'case' entry per <c>INotification</c> the <c>Mediator</c> is interested in. 
        ///     </para>
        /// </remarks>
        override public void HandleNotification(INotification notification)
        {
            switch (notification.Name) {
            }
            /*
            Point? newCameraPosition = null;
            switch (notification.Name) {
                case DesktopMediator.KEY_UP:
                    break;
                case DesktopMediator.KEY_DOWN:
                    switch (((KeyEventArgs)notification.Body).Key) { 
                        case Key.PageUp:
                            if (MyFacade.Instance.MyWorld.Map.Camera.Scale + cameraScaleStep < maxCameraScale)
                                MyFacade.Instance.MyWorld.Map.Camera.Scale = MyFacade.Instance.MyWorld.Map.Camera.Scale + cameraScaleStep;
                            break;
                        case Key.PageDown:
                            if (MyFacade.Instance.MyWorld.Map.Camera.Scale - cameraScaleStep > minCameraScale)
                                MyFacade.Instance.MyWorld.Map.Camera.Scale = MyFacade.Instance.MyWorld.Map.Camera.Scale - cameraScaleStep;
                            break;
                        case Key.Left:
                            {
                                newCameraPosition = new Point(
                                    (int)(MyFacade.Instance.MyWorld.Map.Camera.Position.X - cameraMoveStep),
                                    (int)(MyFacade.Instance.MyWorld.Map.Camera.Position.Y - cameraMoveStep));
                            }
                            break;
                        case Key.Right:
                            {
                                newCameraPosition = new Point(
                                    (int)(MyFacade.Instance.MyWorld.Map.Camera.Position.X + cameraMoveStep),
                                    (int)(MyFacade.Instance.MyWorld.Map.Camera.Position.Y + cameraMoveStep));
                            }
                            break;
                        case Key.Up:
                            {
                                newCameraPosition = new Point(
                                    (int)(MyFacade.Instance.MyWorld.Map.Camera.Position.X - cameraMoveStep),
                                    (int)(MyFacade.Instance.MyWorld.Map.Camera.Position.Y + cameraMoveStep));
                            }
                            break;
                        case Key.Down:
                            {
                                newCameraPosition = new Point(
                                    (int)(MyFacade.Instance.MyWorld.Map.Camera.Position.X + cameraMoveStep),
                                    (int)(MyFacade.Instance.MyWorld.Map.Camera.Position.Y - cameraMoveStep));
                            }
                            break;
                    }
                    break;
            }
            if (newCameraPosition.HasValue)
            {
                int x = (int)newCameraPosition.Value.X;
                int y = (int)newCameraPosition.Value.Y;

                
                 //y>=-x+maxy
                 //y<=-x+(miny+maxy)
                 
                 //y<=x+minX
                 //y>=x-minX
                  
                 //x>= -y+maxy
                 //x<=-y+miny+maxy
                  
                 //x>y-minX
                 //x<y+minX
                 

                if (!(x >= -y + maxCameraY && x <= -y + 2*minCameraY + maxCameraY && x > y - minCameraX && x < y + minCameraX)
                    || 
                    !(y >= -x + maxCameraY && y <= -x + 2*minCameraY + maxCameraY && y <= x + minCameraX && y >= x - minCameraX))
                {
                    x = (int)MyFacade.Instance.MyWorld.Map.Camera.Position.X;
                    y = (int)MyFacade.Instance.MyWorld.Map.Camera.Position.Y;
                }
                MyFacade.Instance.MyWorld.Map.Camera.Position = new Point(x, y);
            }
            */base.HandleNotification(notification);
        }

        /// <summary>
        /// Called by the View when the Mediator is registered
        /// </summary>
        override public void OnRegister()
        {
            base.OnRegister();
            ViewComponent.OnExternalActionButtonClick += new OnButtonClickHandler<string>(ViewComponent_OnButtonClick);
            //ViewComponent.SetMapView(mapMediator.ViewComponent);
            //add dos items existentes?
            //itemMenuMediator.
            //itemMediator.OnPositionChange += new Action<IThingDataProxy<MyIdType>>(itemMediator_OnPositionChange);
            //ViewComponent.MyOnSizeChanged += new Action<Point>(ViewComponent_OnSizeChanged);
            //itemMediator.OnDirectionChange += new Action<IThingDataProxy<ItemVOId>>(itemMediator_OnDirectionChange);
        }

        /// <summary>
        /// Called by the View when the Mediator is removed
        /// </summary>
        override public void OnRemove()
        {
            base.OnRemove();
            //Facade.RemoveMediator(mapMediator.MediatorName);
            //Facade.RemoveMediator(Menu.MediatorName);
            // Facade.RemoveMediator(MenuBar.MediatorName);
        }

        #endregion

        //TODO meter isto num proxy para depois ir para bd
        const int maxCameraX = 100;
        const int minCameraX = 50;
        const int maxCameraY = 100;
        const int minCameraY = 50;
        const float maxCameraScale = 10;
        const float minCameraScale = 2.25f;
        const float cameraScaleStep = 0.25f;
        const float cameraMoveStep = 1f;

        void ViewComponent_OnButtonClick(string text)
        {
            switch (text)
            {
                case MyGameUC.SAVEBUTTON:
                    MyFacade.Instance.saveGame();
                    //aparece ecra com o que se pode adicionar
                    //SendNotification(MyFacade.PAGE_GAME_NEWTHING_IN_COMMAND);
                    break;
                case MyGameUC.OPTIONSBUTTON:
                    MessageBox.Show("TODO");
                    break;
                case MyGameUC.EXITBUTTON:
                    SendNotification(MyFacade.PAGE_GAME_OUT_COMMAND);
                    break;
            }
        }
    }
}
